<?php

/**
 * Excel  读取
 */
class doExcel
{
    public function readExcel($file_path, $page)
    {
        error_reporting(E_ALL);
        date_default_timezone_set('Asia/ShangHai');

        /** PHPExcel_IOFactory */
        include_once ROOT_PATH . 'inc/phpExcel/Classes/PHPExcel/IOFactory.php';


        // Check prerequisites
        if (!file_exists($file_path)) {
            exit("not found");
        }

        $reader = PHPExcel_IOFactory::createReader('Excel5'); //设置以Excel5格式(Excel97-2003工作簿)
        $PHPExcel = $reader->load($file_path); // 载入excel文件
        $sheet = $PHPExcel->getSheet($page); // 读取第一個工作表
        $highestRow = $sheet->getHighestRow(); // 取得总行数
        $highestColumm = $sheet->getHighestColumn(); // 取得总列数
        $highestColumm = PHPExcel_Cell::columnIndexFromString($highestColumm);
        if ($highestColumm != 30) {
            return false;
        } else {
            /** 循环读取每个单元格的数据 */
            $dataset = [];
            for ($row = 1; $row <= $highestRow; $row++) {//行数是以第1行开始
                for ($column = 'A'; PHPExcel_Cell::columnIndexFromString($column) <= $highestColumm; $column++) {//列数是以A列开始
                    $dataset[] = $sheet->getCell($column . $row)->getValue();
                    // echo $column.$row.":".$sheet->getCell($column.$row)->getValue()."<br />";
                }
            }
            return $dataset;
        }
    }

    public function readExcelByAdZoneId($file_path, $page)
    {
        error_reporting(E_ALL);
        date_default_timezone_set('Asia/ShangHai');

        /** PHPExcel_IOFactory */
        include ROOT_PATH . 'inc/phpExcel/Classes/PHPExcel/IOFactory.php';


        // Check prerequisites
        if (!file_exists($file_path)) {
            exit("not found");
        }

        $reader = PHPExcel_IOFactory::createReader('Excel5'); //设置以Excel5格式(Excel97-2003工作簿)
        $PHPExcel = $reader->load($file_path); // 载入excel文件
        $sheet = $PHPExcel->getSheet($page); // 读取第一個工作表
        $highestRow = $sheet->getHighestRow(); // 取得总行数
        $highestColumm = $sheet->getHighestColumn(); // 取得总列数
        $highestColumm = PHPExcel_Cell::columnIndexFromString($highestColumm);
        if ($highestColumm != 2) {
            return false;
        } else {
            /** 循环读取每个单元格的数据 */
            $dataset = [];
            for ($row = 1; $row <= $highestRow; $row++) {//行数是以第1行开始
                for ($column = 'A'; PHPExcel_Cell::columnIndexFromString($column) <= $highestColumm; $column++) {//列数是以A列开始
                    $dataset[] = $sheet->getCell($column . $row)->getValue();
                    // echo $column.$row.":".$sheet->getCell($column.$row)->getValue()."<br />";
                }
            }
            return $dataset;
        }
    }

    public function exportExcelForApplyCash()
    {
        error_reporting(E_ALL);
        date_default_timezone_set('Asia/ShangHai');

        /** PHPExcel_IOFactory */
        include ROOT_PATH . 'inc/phpExcel/Classes/PHPExcel/IOFactory.php';
        include ROOT_PATH . 'inc/phpExcel/Classes/PHPExcel.php';
        $objPHPExcel = new PHPExcel();
        //$iofactory=new PHPExcel_IOFactory();
        //获得数据  ---一般是从数据库中获得数据
        $data = array(
            0 => array('id' => 2013, 'name' => '张某某', 'age' => 21),
            1 => array('id' => 201, 'name' => 'EVA', 'age' => 21)
        );
        //设置excel列名
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', '编号');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B1', '姓名');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('C1', '年龄');
        //把数据循环写入excel中
        foreach ($data as $key => $value) {
            $key += 2;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A' . $key, $value['id']);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B' . $key, $value['name']);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('C' . $key, $value['age']);
        }
        //excel保存在根目录下  如要导出文件，以下改为注释代码
        // $objPHPExcel->getActiveSheet() -> setTitle('SetExcelName');
        // $objPHPExcel-> setActiveSheetIndex(0);
        // $objWriter = $iofactory -> createWriter($objPHPExcel, 'Excel2007');
        // $objWriter -> save('SetExcelName.xlsx');
        //导出代码
        $objPHPExcel->getActiveSheet()->setTitle('SetExcelName');
        $objPHPExcel->setActiveSheetIndex(0);
        $filename = 'SetExcelName';
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $filename . '.xls"');
        header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory:: createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save('php://output');

    }
}
   

